package com.cqcj.login.service;

import com.cqcj.login.mapper.BrandMapper;
import com.cqcj.login.pojo.Brand;
import com.cqcj.login.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import java.util.List;


public class BrandService {
    SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getSqlSessionFactory();

    public List<Brand> selectAll(){
        //通过mybatis配置文件连接数据库
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        //调用mapper在数据库中找有没有账号密码相同的用户
        System.out.println("查数据库啦！");
        List<Brand> brands = mapper.selectAll();
        sqlSession.close();
        return brands;
    }

    public Brand selectById(int id){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        //调用mapper在数据库中找有没有账号密码相同的用户
        Brand brand = mapper.selectById(id);
        sqlSession.close();
        return brand;
    }

    public int insetBrand(Brand brand){
        //通过mybatis配置文件连接数据库
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        //调用mapper在数据库中找有没有账号密码相同的用户
        int flag = mapper.insetBrand(brand);
        //提交事务
        sqlSession.commit();
        sqlSession.close();
        return flag;
    }

    public void updateBrand(Brand brand){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        mapper.updateBrand(brand);
        sqlSession.commit();
        sqlSession.close();
    }

    public void deleteBrand(int id){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        mapper.deleteBrand(id);
        sqlSession.commit();
        sqlSession.close();
    }
}
